<?php
/**
 * User: iLock
 * Date: 2017/11/15
 * Time: 17:08
 */

namespace app\index\home;

use think\Log;	
use think\Db;
use think\Validate;
use app\index\home\Base;
// use think\Cache;
class Wxpay extends \think\Controller
{	
	protected function initialize()
    {
       parent::initialize();
       
    }
	// 获取微信支付二维码
	function getPayCode($data){
		$validate = new Validate([
			['out_trade_no','require|alphaNum','订单编号错误|订单编号错误'],
			['total_fee','require|number|gt:0','金额错误|金额错误|金额错误'],
			['subject','require','请输入标题'],
			['body','require','请输入描述']

		]);
		if(!$validate->check($data)){
			return ['code'=>0,'msg'=>$validate->getError()];
		}
        import('wxpay.example.NativePay',EXTEND_PATH);
        import('wxpay.lib.WxPayApi',EXTEND_PATH);
        // require_once "../lib/WxPay.Api.php";
        $input = new \WxPayUnifiedOrder();
        $notify = new \NativePay();
		$input->SetBody($data['body']);
		// $input->SetAttach("test");
		$input->SetOut_trade_no($data['out_trade_no']);
		$input->SetTotal_fee($data['total_fee']*100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		// $input->SetGoods_tag("test");
		$input->SetNotify_url("http://shop2.100bit.cn/index.php/index/Wxpay/pay_notify");
		$input->SetTrade_type("NATIVE");
		$input->SetProduct_id($data['out_trade_no']);
		$result = $notify->GetPayUrl($input);
		$url = $result["code_url"];
		if($url){
			 return ['code'=>1,'msg'=>$url];
		}
		else{
			return ['code'=>0,'msg'=>'创建订单二维码失败!!!'];
		}
	}

	function notify(){
		$xmls = file_get_contents("php://input");
		file_put_contents('20220512.txt',json_encode($xmls));
		// Log::error("== test更新失败 =="); 
		// exit;
		$r1_MerchantNo = input('r1_MerchantNo/s');
		$r2_OrderNo = input('r2_OrderNo/s');
		$r3_Amount = input('r3_Amount');
		$r6_Status = input('r6_Status/d');
		$r7_TrxNo = input('r7_TrxNo/s');
		$ra_PayTime = input('ra_PayTime');
		$rb_DealTime = input('rb_DealTime');
		$rc_BankCode = input('rc_BankCode');
		$rd_OpenId = input('rd_OpenId');
		$hmac = input('hmac');

		$pay_time = urldecode(urldecode($ra_PayTime));
		$deal_time = urldecode(urldecode($rb_DealTime));
		// Log::error($pay_time);exit;
		// Log::error($r1_MerchantNo."-----".$r2_OrderNo."-----".$r3_Amount."-----".$r6_Status."-----".$r7_TrxNo."-----".$pay_time."-----".$deal_time."-----".$rc_BankCode."-----".$rd_OpenId."-----".$hmac."-----");
		if($r6_Status==100){
			// 支付成功
			$data['pay_status'] = 1;
			$data['transaction_id'] = $r1_MerchantNo;
			$data['pay_time'] = strtotime($pay_time);
			$data['huiju_deal_time'] = strtotime($deal_time);
			$data['buy_time'] = time();
			$rs = Db::name('orders')->where('orders_id',$r2_OrderNo)->update($data);

			$order = Db::name('orders')->where('orders_id',$r2_OrderNo)->find();
			if(!$order){
				echo 'error';
				Log::error("无此订单".$r1_MerchantNo."-----".$r2_OrderNo."-----".$r3_Amount."-----".$r6_Status."-----".$r7_TrxNo."-----".$pay_time."-----".$deal_time."-----".$rc_BankCode."-----".$rd_OpenId."-----".$hmac."-----");
				exit;
			}
			$quan_id = $order['quan_id'];
	        if($quan_id>0){
	        	Db::name('user_quan')->where('id',$quan_id)->update('status',1);
	        }

	        $jf['orders_id'] = $order['id'];
			$jf['price'] = $order['order_price'];
			$jf['jifen'] = intval($jf['price']*1000/10);
			$jf['uid'] = $order['uid'];
			$jf['ctime'] = time();
			// 变更积分和用户等级
			$record = Db::name('user_jifen_record')->where('orders_id',$jf['orders_id'])->value('id');
			if(!$record&&$jf['jifen']>0){
				$rs = Db::name('user_jifen_record')->insert($jf);
				$nowJf = Db::name('user')->where('id',$order['uid'])->value('integral');
				$newJf['integral'] = $nowJf+$jf['jifen'];
				$rs1 = Db::name('user')->where('id',$order['uid'])->update($newJf);

				$level = Db::name('user')->where('id',$order['uid'])->value('level');
				$where['jifen'] = array('elt',$newJf['integral']);
				$nextLevel = Db::name('jifen_set')->where($where)->order('id desc')->limit(1)->value('level');
				if($nextLevel&&$level<$nextLevel){
					Db::name('user')->where('id',$order['uid'])->update(array('level'=>$nextLevel));
				}

			}
			else{
				trace('积分添加出错'.$jf['jifen'],'error');
			}

			// 通知管理员
			$msg_order_id = substr($order['orders_id'], 0,13)."***";
			$rs1 = sendNotice('13602856519',$msg_order_id,$order['keshi'],$order['bed_num'],$order['contract_phone']);
			if($order['hid']==11){
				$rs4 = sendNotice('18475984003',$msg_order_id,$order['keshi'],$order['bed_num'],$order['contract_phone']);//刘雪梅
			}
			// sendNotice('15920170642',$msg_order_id,$order['keshi'],$order['bed_num'],$order['contract_phone']);
			// $rs2 = sendNotice('13631335486',$msg_order_id,$order['keshi'],$order['bed_num'],$order['contract_phone']);



			echo 'success';
			exit;
		}
		else{
			echo 'error';
			Log::error("支付失败".$r1_MerchantNo."-----".$r2_OrderNo."-----".$r3_Amount."-----".$r6_Status."-----".$r7_TrxNo."-----".$pay_time."-----".$deal_time."-----".$rc_BankCode."-----".$rd_OpenId."-----".$hmac."-----");
			exit;
		}
		
	}

	function notify1(){
		//存储微信的回调
		$xmls = file_get_contents("php://input");	
		ob_start();
		// file_put_contents('a1.txt',json_encode($xmls));
		echo $xmls;
		// file_put_contents("payres_circle.txt",ob_get_clean());
		//使用simplexml_load_string() 函数将接收到的XML消息数据载入对象$postObj中。
		if(!empty($xmls)){
			$postObj = simplexml_load_string($xmls, 'SimpleXMLElement', LIBXML_NOCDATA);
			$result_code = $postObj->result_code; //SUCCESS
			$openid = $postObj->openid;
			$out_trade_no = $postObj->out_trade_no;
			$transaction_id = $postObj->transaction_id;
			//file_put_contents('error.txt',"(支付)time:".$out_trade_no.'-'.$result_code,FILE_APPEND);
			if($result_code == "SUCCESS") {
				if(!empty($out_trade_no)){
					$rs = Db::name('orders')->where('orders_id',$out_trade_no)->update(array('pay_status'=>1,'pay_time'=>time(),'transaction_id'=>$transaction_id));

					// 购买量+1
					// Db::name('product')->where('id',$product_id)->setInc('buy_count');

					// 库存减一
					// Db::name('product')->where('id',$product_id)->setDec('rest_num');
					

					$order_info = Db::name('orders')->where('orders_id',$out_trade_no)->find();

					// 减库存
					if($order_info['is_slcg']==0){
						$goods_id = $order_info['goods_id'];
						$goods_num = $order_info['goods_num'];
						$goods_price = $order_info['goods_price'];
						$goods_id_arr = explode(',',$goods_id);
						$goods_num_arr = explode(',',$goods_num);
						$goods_price_arr = explode(',',$goods_price);
						$spec_arr = explode(',',$order_info['goods_spec']);
						foreach($goods_id_arr as $k=>&$v){
							$buy_num = intval($goods_num_arr[$k]);

							$goods_info = Db::name('product')->where('id',$v)->field('spec,rest_num,spec_num')->find();
							$goods_spec_arr = explode(',',$goods_info['spec']);
							$spec_num = explode(',',$goods_info['spec_num']);
							// $goods_rest_num_arr = explode(',',$goods_info['rest_num']);
							$rest_num_del = 0;
							foreach($goods_spec_arr as $kk=>&$vv){
								$rest_num_del += $buy_num*$spec_num[$kk];
								// if($vv==$spec_arr[$k]){
								// 	$goods_rest_num_arr[$kk] -= $buy_num;
								// }
							}

							Db::name('product')->where('id',$v)->setDec('rest_num',$rest_num_del);
							// $p_rest_num_str = implode(',',$goods_rest_num_arr);
							// Db::name('product')->where('id',$v)->update(array('rest_num'=>$p_rest_num_str));
							// Db::name('product')->where('id',$v)->setDec('rest_num',$buy_num);
							Db::name('product')->where('id',$v)->setInc('buy_count',$rest_num_del);
							$sell_in_data = array(
								'gid'=>$v,
								'sell_num'=>$goods_num_arr[$k],
								'sell_money'=>$goods_price_arr[$k]*$goods_num_arr[$k],
								'ctime'=>time()
							);
							Db::name('product_sell_record')->insert($sell_in_data);
						}
					}
					else{
						// 有包含采购送礼
						$goods_id = $order_info['goods_id'];
						$goods_num = $order_info['goods_num'];
						$goods_price = $order_info['goods_price'];
						$goods_spec = $order_info['goods_spec'];

						$goods_id_arr = explode(',',$goods_id);
						$goods_num_arr = explode(',',$goods_num);
						$goods_price_arr = explode(',',$goods_price);
						$goods_spec_arr = explode(',',$goods_spec);

						foreach($goods_id_arr as $k=>&$v){
							$goods_info = Db::name('product')->where('id',$v)->field('slcg_spec,slcg_price,spec,rest_num,spec_num,unit_transform')->find();
							$buy_num = intval($goods_num_arr[$k]);
							// 采购送礼减库存
							if($goods_spec_arr[$k]==$goods_info['slcg_spec']){
								$rest_num_del = $goods_info['unit_transform']*$buy_num;
								Db::name('product')->where('id',$v)->setDec('rest_num',$rest_num_del);
								
								$sell_money = $goods_info['slcg_price']*$buy_num;
							}
							else{
								$goods_spec_arr = explode(',',$goods_info['spec']);
								$spec_num = explode(',',$goods_info['spec_num']);
								// $goods_rest_num_arr = explode(',',$goods_info['rest_num']);
								$rest_num_del = 0;
								foreach($goods_spec_arr as $kk=>&$vv){
									$rest_num_del += $buy_num*$spec_num[$kk];
									// if($vv==$spec_arr[$k]){
									// 	$goods_rest_num_arr[$kk] -= $buy_num;
									// }
								}
								Db::name('product')->where('id',$v)->setDec('rest_num',$rest_num_del);
								// Db::name('product')->where('id',$v)->setDec('rest_num',$buy_num);
								$sell_money = $goods_price_arr[$k]*$buy_num;
							}
							Db::name('product')->where('id',$v)->setInc('buy_count',$rest_num_del);

							$sell_in_data = array(
								'gid'=>$v,
								'sell_num'=>$goods_num_arr[$k],
								'sell_money'=>$sell_money,
								'ctime'=>time()
							);
							Db::name('product_sell_record')->insert($sell_in_data);
						}
					}

					
					
					
				}
				else{
					file_put_contents('error.txt',"(支付失败)time:".date(''),FILE_APPEND);exit;
				}
			}else{
				// $app->action('shopping','pay_successs_tatus2',array('order_sn'=>$out_trade_no,'status'=>'1'));
				file_put_contents('error.txt',"(支付失败)time:".date(''),FILE_APPEND);exit;
			}
			
		}
	}

	// 积分商城购买成功回调函数
	function notify2(){
		//存储微信的回调
		$xmls = file_get_contents("php://input");	
		ob_start();
		file_put_contents('a2.txt',json_encode($xmls));
		echo $xmls;
		// file_put_contents("payres_circle.txt",ob_get_clean());
		//使用simplexml_load_string() 函数将接收到的XML消息数据载入对象$postObj中。
		if(!empty($xmls)){
			$postObj = simplexml_load_string($xmls, 'SimpleXMLElement', LIBXML_NOCDATA);
			$result_code = $postObj->result_code; //SUCCESS
			$openid = $postObj->openid;
			$out_trade_no = $postObj->out_trade_no;
			$transaction_id = $postObj->transaction_id;
			//file_put_contents('error.txt',"(支付)time:".$out_trade_no.'-'.$result_code,FILE_APPEND);
			if($result_code == "SUCCESS") {
				if(!empty($out_trade_no)){
					$rs = Db::name('orders')->where('orders_id',$out_trade_no)->update(array('pay_status'=>1,'pay_time'=>time()));

					$order_info = Db::name('orders')->where('orders_id',$out_trade_no)->find();

					
					// 库存处理
					$goods_info = Db::name('product')->where('id',$order_info['goods_id'])->field('rest_num,spec,spec_unit,unit_transform,slcg_spec')->find();
					if($order_info['goods_spec']==$goods_info['slcg_spec']){
						$rest_num_del = $goods_info['unit_transform']*$order_info['goods_num'];
					}
					else{
						$spec_arr = explode(',',$goods_info['spec']);
						$spec_unit = explode(',',$goods_info['spec_unit']);
						$rest_num_del = 0;
						foreach($spec_arr as $k=>$v){
							if($order_info['goods_spec']==$v){
								$rest_num_del = intval($spec_unit[$k])*intval($orders_info['goods_num']);
							}
						}
					}
					
					Db::name('product')->where('id',$order_info['goods_id'])->setDec('rest_num',$rest_num_del);
					// Db::name('product')->where('id',$order_info['goods_id'])->update(array('rest_num'=>$rest_num));

					// 购买量+1
					// $goods_num = intval($order_info['goods_num']);
					Db::name('product_jifen')->where('pid',$order_info['goods_id'])->setInc('buy_count',$rest_num_del);

					// 购买量+1
					Db::name('product')->where('id',$order_info['goods_id'])->setInc('buy_count',$rest_num_del);
				}
				else{
					file_put_contents('error.txt',"(支付失败)time:".date(''),FILE_APPEND);exit;
				}
			}else{
				// $app->action('shopping','pay_successs_tatus2',array('order_sn'=>$out_trade_no,'status'=>'1'));
				file_put_contents('error.txt',"(支付失败)time:".date(''),FILE_APPEND);exit;
			}
			
		}
	}
	// 微信支付异步回调
	function pay_notify(){
		import('wxpay/lib/WxPayApi',EXTEND_PATH);//引入notify类
		import('wxpay/lib/WxPayNotify',EXTEND_PATH);//引入notify类
		import('wxpay/example/notify',EXTEND_PATH);//引入notify类
		$notify = new \PayNotifyCallBack();
    	$notify->Handle(false); //调用方法
	}

	// 微信支付异步回调
	function app_pay_notify(){
		import('wxapppay/lib/WxPayApi',EXTEND_PATH);//引入notify类
		import('wxapppay/lib/WxPayNotify',EXTEND_PATH);//引入notify类
		import('wxapppay/example/notify',EXTEND_PATH);//引入notify类
		$notify = new \PayNotifyCallBack();
    	$notify->Handle(false); //调用方法
	}

	// 订单查询
	function checkOrder($order_id){
		$tranType = '100';
		$javaCharset = 'UTF-8';
		$version = 'V60';
		$userid = config('WXUSERID');
		$pwd = config('WXPWD');
		$merId = config('MerId');
		$orderNo = $order_id;
		$url = 'https://www.gnetpg.com/GneteMerchantAPI/Trans.action';
		$data = "Version=".$version."&TranType=".$tranType."&JavaCharset=".$javaCharset."&MerId=".$merId."&UserId=".$userid."&Pwd=".$pwd."&OrderNo=".$orderNo;
		$res = curl_http_request($url,$data);
		var_dump($res);exit;
	}

	// 微信JSAPI支付
	function jsPay($data){
		$validate = new Validate([
			['out_trade_no','require|alphaNum','订单编号错误|订单编号错误'],
			['total_fee','require|number|gt:0','金额错误|金额错误|金额错误'],
			['subject','require','请输入标题'],
			['body','require','请输入描述']

		]);
		if(!$validate->check($data)){
			return ['code'=>0,'msg'=>$validate->getError()];
		}
        import('wxpay.lib.WxPayApi',EXTEND_PATH);
        import('wxpay.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
        // require_once "../lib/WxPay.Api.php";
        
		//①、获取用户openid
		$tools = new \JsApiPay();
		$openId = session('user_openid');
		if(!$openId){
			echo "openidNull";
			exit;
		}

		//②、统一下单
		$input = new \WxPayUnifiedOrder();
		$input->SetBody($data['body']);
		// $input->SetAttach("test");
		$input->SetOut_trade_no($data['out_trade_no']);
		$input->SetTotal_fee($data['total_fee']*100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		// $input->SetGoods_tag("test");
		$input->SetNotify_url("http://".config("host")."/index/Wxpay/pay_notify");
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = \WxPayApi::unifiedOrder($input);
		// var_dump($order);exit;
		$jsApiParameters = $tools->GetJsApiParameters($order);

		//获取共享收货地址js函数参数
		$editAddress = $tools->GetEditAddressParameters();


		if($order['result_code']=="SUCCESS"&&$order['return_code']){
			$arr = array('code'=>1,'jsApiParameters'=>$jsApiParameters,'editAddress'=>$editAddress,'msg'=>'ok');
		}
		else{
			$arr = array('code'=>0,'msg'=>$order['return_msg']);
		}
		return $arr;
		// echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
		// $this->printf_info($order);
		// $jsApiParameters = $tools->GetJsApiParameters($order);

		// //获取共享收货地址js函数参数
		// $editAddress = $tools->GetEditAddressParameters();
	}

	//打印输出数组信息
	function printf_info($data)
	{
	    foreach($data as $key=>$value){
	        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
	    }
	}

	function getIndexOpenid(){
		import('wxpay.lib.WxPayApi',EXTEND_PATH);
		import('wxpay.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
		$tools = new \JsApiPay();
		
		$openId = $tools->GetOpenid();
		// echo $openId;exit;
		return $openId;
	}

	// H5支付
	function otherWxPay($data){
		$validate = new Validate([
			['out_trade_no','require|alphaNum','订单编号错误|订单编号错误'],
			['total_fee','require|number|gt:0','金额错误|金额错误|金额错误'],
			['subject','require','请输入标题'],
			['body','require','请输入描述']

		]);
		if(!$validate->check($data)){
			return ['code'=>0,'msg'=>$validate->getError()];
		}
        import('wxpay.example.NativePay',EXTEND_PATH);
        import('wxpay.lib.WxPayApi',EXTEND_PATH);
        // require_once "../lib/WxPay.Api.php";
        $input = new \WxPayUnifiedOrder();
        $notify = new \NativePay();
		$input->SetBody($data['body']);
		$input->SetSpbill_create_ip($data['cip']);
		$input->SetOut_trade_no($data['out_trade_no']);
		$input->SetTotal_fee($data['total_fee']*100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		// $input->SetGoods_tag("test");
		$input->SetNotify_url("http://shop2.100bit.cn/index.php/index/Wxpay/pay_notify");
		$input->SetTrade_type("MWEB");
		$input->SetProduct_id($data['out_trade_no']);
		$result = $notify->GetPayUrl($input);
		// var_dump($result);exit();
		$url = $result["mweb_url"];
		if($url){
			 return ['code'=>1,'msg'=>$url];
		}
		else{
			return ['code'=>0,'msg'=>'创建订单失败!!!'];
		}
	}

	// ios微信支付
	function iosPay($data){
		$validate = new Validate([
			['out_trade_no','require|alphaNum','订单编号错误|订单编号错误'],
			['total_fee','require|number|gt:0','金额错误|金额错误|金额错误'],
			['subject','require','请输入标题'],
			['body','require','请输入描述']

		]);
		if(!$validate->check($data)){
			return ['code'=>0,'msg'=>$validate->getError()];
		}
        import('wxapppay.lib.WxPayApi',EXTEND_PATH);

        $input = new \WxPayUnifiedOrder();
		$input->SetBody($data['body']);
		// $input->SetAttach("test");
		$input->SetOut_trade_no($data['out_trade_no']);
		$input->SetTotal_fee($data['total_fee']*100);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		// $input->SetGoods_tag("test");
		$input->SetNotify_url("http://shop2.yyping.cn/index.php/index/Wxpay/app_pay_notify");
		$input->SetTrade_type("APP");
		// $input->SetOpenid($openId);
		$order = \WxPayApi::unifiedOrder($input);
		// var_dump($order);exit;
		$nonceStr = sp_random_string(32);
		$timeStamp = time();
		$params = array('appid'=>$order['appid'],'partnerid'=>config('APPMCHID'),'prepayid'=>$order['prepay_id'],'package'=>'Sign=WXPay','noncestr'=>$nonceStr,'timestamp'=>$timeStamp);
		$key = config('APPKEY');
		$newSign = $this->MakeSign($params,$key);
		$params['sign'] = $newSign;
		out_api($params);
	}

	function MakeSign($params, $KEY)
    {
        //签名步骤一：按字典序排序数组参数
        ksort($params);
        $buff1 = '';
        foreach ($params as $k => $v) {
            if ($k != "sign" && $v != "" && !is_array($v)) {
                $buff1 .= $k . "=" . $v . "&";
            }
        }
        $buff1 = trim($buff1, "&");
        //签名步骤二：在string后加入KEY
        $string = $buff1 . "&key=" . $KEY;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);

        return $result;
    }

    public function refund($orders_id){
    	$p1_MerchantNo = config('huiju_MerchantNo');
    	$p2_OrderNo = $orders_id;
    	if($p2_OrderNo){
    		$td_order_info = Db::name('orders_tuikuan')->where('orders_id',$p2_OrderNo)->find();
    		if(!$td_order_info){
    			errMsg('无此订单');
    		}
    		else{
    			$p3_RefundOrderNo = $td_order_info['refund_id'];
    			$p4_RefundAmount = $td_order_info['reback_money'];
    			$p5_RefundReason = $td_order_info['reason'];
    		}
    	}
    	// $p3_RefundOrderNo = "myrefund_".rand(1,9999);
    	// $p4_RefundAmount = 1;
    	// $p5_RefundReason = "测试";
    	$p6_NotifyUrl = "http://".config("host")."/index/Wxpay/refund_notify";
    	$q1_version = "2.0";
    	$data = $p1_MerchantNo.$p2_OrderNo.$p3_RefundOrderNo.$p4_RefundAmount.$p5_RefundReason.$p6_NotifyUrl.$q1_version;
    	$hmac = getHmac($data);
    	$return_data = array("p1_MerchantNo"=>$p1_MerchantNo,"p2_OrderNo"=>$p2_OrderNo,"p3_RefundOrderNo"=>$p3_RefundOrderNo,"p4_RefundAmount"=>$p4_RefundAmount,"p5_RefundReason"=>$p5_RefundReason,"p6_NotifyUrl"=>$p6_NotifyUrl,'q1_version'=>$q1_version,'hmac'=>$hmac);
    	
    	$res = juhecurl('https://www.joinpay.com/trade/refund.action',$return_data,1);
    	// $rs = $this->refund_check("20201021QNcctKRP");
    	// var_dump($rs);exit;
    	if($res){
    		$res_arr = json_decode($res,true);
    		if($res_arr['rb_Code']==100){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    	else{
    		return false;
    	}
    	// var_dump($res);exit;
    }

    public function refund_notify(){
    	$data = input("get.");
    	if($data){
    		if($data['ra_Status']==100){
    			$where['orders_id'] = $data['r2_OrderNo'];
    			$dt['huiju_refund_id'] = $data['r5_RefundTrxNo'];
    			$dt['refund_status'] = 1;
    			$dt['refund_msg'] = $data['rc_CodeMsg'];
    			$dt['refund_success_time'] = time();
    		}
    		else{
    			$where['orders_id'] = $data['r2_OrderNo'];
    			$dt['refund_msg'] = $data['rc_CodeMsg'];
    		}
    		$rs = Db::name('orders_tuikuan')->where($where)->update($dt);
    		$dt = json_encode($data);
    		Log::error("refund_notify:".$dt);
    		echo "success";
    	}
    	else{
    		Log::error("nodata:");
    	}
    	echo 'success';
    }

    public function refund_check($refund_id){
    	$p1_MerchantNo = config('huiju_MerchantNo');
    	$p2_RefundOrderNo = $refund_id;
    	$p3_Version = "3.0";
    	$data = $p1_MerchantNo.$p2_RefundOrderNo.$p3_Version;
    	$hmac = getHmac($data);
    	$return_data = array("p1_MerchantNo"=>$p1_MerchantNo,"p2_RefundOrderNo"=>$p2_RefundOrderNo,"p3_Version"=>$p3_Version,'hmac'=>$hmac);
    	
    	$res = juhecurl('https://www.joinpay.com/trade/queryRefund.action',$return_data,1);
    	var_dump($res);exit;
    }
}
?>